/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Fragment} from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,

  Button,
  
  StatusBar,
} from 'react-native';
import { TextInput } from 'react-native-gesture-handler';

export default class FetchDemoPage extends React.Component{
  constructor(props){
    super(props)
    this.state={
      showText:'11'
    }
  }

  loadData(){

    let url=`https://api.github.com/search/repositories?q=${this.searchKey}` 

    fetch(url)
    .then(response=>{console.log("sss",response);return response.text()})
    .then(responseText=>{
      this.setState({
        showText:responseText
      })
    })

  }

  loadData2(){

    let url=`https://api.github.com/search/repositories?q=${this.searchKey}` 
  
    fetch(url)
    .then(response=>{
      if(response.ok){
        return res.text()
      }
      throw new Error('Network response was not ok.')
    })
    .then(responseText=>{
      this.setState({
        showText:responseText
      })
    })
    .catch(e=>{
      this.setState({
        showText:e.toString()
      })
    })

  }
  render(){
    return (
      <View style={styles.sectionContainer}>
        <Text style={styles.welcome}>Fetch 使用</Text>
        <View style={styles.inputWrap}>
          <TextInput 
            style={styles.input}
            onChangeText={text=>{
              this.searchKey=text
            }}
          />
          <Button 
            title={'获取'}
            onPress={()=>{
              this.loadData2()
            }}
          />

        </View>
     
        <Text >{this.state.showText}</Text>
      </View>
    )
  }
 
};

const styles = StyleSheet.create({

  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color:'#333',
  },
  inputWrap:{
    flexDirection:'row',
    alignItems:'center'


  },
  input:{
    flex:1,
    marginRight:30,
    height:40,
    lineHeight:40,
    borderColor:'black',
    borderWidth:1
  }
 
});


